package com.service.order.service.serviceImpl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.model.order.pojo.entity.Order;
import com.model.product.pojo.entity.Product;
import com.service.order.mapper.OrderMapper;
import com.service.order.service.OrderService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Service
@Slf4j
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements OrderService {
    @Resource
    DiscoveryClient discoveryClient;
    @Resource
    RestTemplate restTemplate;
    @Resource
    LoadBalancerClient loadBalancerClient;

    /**
     * 获取商品信息-普通版
     * @param id
     * @return
     */
    public Product getProductById(Long id) {
        // 获取实例列表
        List<ServiceInstance> instanceList = discoveryClient.getInstances("product-service");
        // 获取服务压力最小的实例（这里模拟用第一个代替）
        ServiceInstance instance = instanceList.get(0);
        // 获取实例的ip和端口号
        String url = "http://" + instance.getHost() + ":" + instance.getPort() + "/product/getProductById/" + id;
        log.info("请求地址：" + url);
        // 发送请求，返回结果
        return restTemplate.getForObject(url, Product.class);
    }

    /**
     * 方法1：通过loadBalancerClient.choose获取负载均衡筛选后的实例来实现负载均衡
     * 获取商品信息-负载均衡
     * @param id
     * @return
     */
    public Product getProductByIdBalancer(Long id) {
        // 负载均衡获取单个实例
        ServiceInstance choose = loadBalancerClient.choose("product-service");
        // 从实例拿到ip和端口
        String url = "http://" + choose.getHost() + ":" + choose.getPort() + "/product/getProductById/" + id;
        log.info("请求地址：" + url);
        // 发送请求，返回结果
        return restTemplate.getForObject(url, Product.class);
    }

    /**
     * 方法2： 通过注解restTemplate来实现负载均衡，需要在配置类中添加@LoadBalanced注解
     * @param id
     * @return
     */
    public Product getProductByIdBalancerAnnotation(Long id) {
        String url = "http://product-service/product/getProductById/" + id;
        log.info("请求地址：" + url);
        // 发送请求，返回结果
        return restTemplate.getForObject(url, Product.class);
    }

    @Override
    public Order getOrderById(Long orderId, Long productId) {
        // 获取商品信息
        Product product = getProductByIdBalancerAnnotation(productId);
        Order order = new Order();
        order.setId(1);
        order.setTotalFee(new BigDecimal("0"));
        order.setProducts(product.getName());
        order.setCreateTime(LocalDateTime.now());
        ArrayList<Product> productArrayList = new ArrayList<>();
        productArrayList.add(product);
        order.setProductList(productArrayList);

        return order;
    }
}
